/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddFoodDialog.java
 *
 * Created on 2009-12-6, 10:19:37
 */
package com.ordering.menu;

import com.ordering.dao.TFoodTypeJpaController;
import com.ordering.dao.TMenuJpaController;
import com.ordering.pojo.TFoodType;
import com.ordering.pojo.TMenu;
import com.ordering.utils.WindowLocationUtils;
import java.awt.Image;
import java.io.File;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author sulan
 */
public class AddFoodDialog extends javax.swing.JDialog {

    private TFoodType foodType;
    private JTree jTree1;
    javax.swing.JScrollPane jScrollPane1;
    private String photoPath;

    /** Creates new form AddFoodDialog */
    public AddFoodDialog(java.awt.Frame parent, boolean modal, javax.swing.JScrollPane jScrollPane1, JTree jTree1, TFoodType foodType) {
        super(parent, modal);
        this.jScrollPane1 = jScrollPane1;
        this.foodType = foodType;
        this.jTree1 = jTree1;
        initComponents();
        WindowLocationUtils.setWindowCenter(this);
        foodTypeText.setText(foodType.getFoodTypeName());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        tMenu1 = new com.ordering.pojo.TMenu();
        jLabel1 = new javax.swing.JLabel();
        foodNameText = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        priceText = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        foodDesc = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        photoLabel = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        foodTypeText = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("添加菜");
        setAlwaysOnTop(true);
        setModal(true);

        jLabel1.setText("菜名:");
        jLabel1.setName("jLabel1"); // NOI18N

        foodNameText.setName("foodNameText"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tMenu1, org.jdesktop.beansbinding.ELProperty.create("${foodName}"), foodNameText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        foodNameText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                foodNameTextActionPerformed(evt);
            }
        });

        jLabel2.setText("种类:");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText("单价:");
        jLabel3.setName("jLabel3"); // NOI18N

        priceText.setName("priceText"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tMenu1, org.jdesktop.beansbinding.ELProperty.create("${price}"), priceText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        priceText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                priceTextActionPerformed(evt);
            }
        });

        jLabel4.setText("描述:");
        jLabel4.setName("jLabel4"); // NOI18N

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        foodDesc.setColumns(20);
        foodDesc.setRows(3);
        foodDesc.setName("foodDesc"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tMenu1, org.jdesktop.beansbinding.ELProperty.create("${description}"), foodDesc, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jScrollPane3.setViewportView(foodDesc);

        jLabel5.setFont(new java.awt.Font("宋体", 1, 18));
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText("添 加 菜");
        jLabel5.setName("jLabel5"); // NOI18N

        photoLabel.setText("请 上 传 图 片");
        photoLabel.setInheritsPopupMenu(false);
        photoLabel.setName("photoLabel"); // NOI18N
        photoLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                photoLabelMouseClicked(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("宋体", 0, 8));
        jButton1.setText("上传");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("添加");
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        foodTypeText.setEditable(false);
        foodTypeText.setEnabled(false);
        foodTypeText.setName("foodTypeText"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(118, 118, 118)
                        .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                        .addGap(108, 108, 108))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(foodNameText, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(foodTypeText)
                                    .addComponent(priceText, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(photoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1))
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(172, 172, 172)
                        .addComponent(jButton2)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {foodNameText, priceText});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 16, Short.MAX_VALUE)
                            .addComponent(foodNameText))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
                            .addComponent(foodTypeText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(41, 41, 41))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(60, 60, 60)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGap(2, 2, 2)
                                            .addComponent(priceText))
                                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)))
                                .addComponent(photoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(9, 9, 9)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addGap(30, 30, 30))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void foodNameTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_foodNameTextActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_foodNameTextActionPerformed

    private void priceTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_priceTextActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_priceTextActionPerformed

    private void photoLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_photoLabelMouseClicked
        // TODO add your handling code here:
}//GEN-LAST:event_photoLabelMouseClicked

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
//        FileInputStream fis = null;
        try {
            TMenuJpaController jpa = new TMenuJpaController();
            tMenu1.setFoodTypeOid(foodType);
            if (photoPath != null && !photoPath.trim().equals("")) {
                File photoFile = new File(photoPath);
                 String extName = photoFile.getName().substring(photoFile.getName().indexOf("."));
                tMenu1.setPhotoName(tMenu1.getFoodName()+extName);
                WindowLocationUtils.copyFile(photoPath, "pic/" + tMenu1.getPhotoName());
            } else {
                tMenu1.setPhotoName(null);
            }
//            fis = new FileInputStream(photoFile);
//            byte[] b = new  byte[(int)photoFile.length()];
//            fis.read(b);
//            tMenu1.setPhoto(b);

            jpa.create(tMenu1);

            DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();
            DefaultMutableTreeNode menuNodeNew = new DefaultMutableTreeNode(tMenu1);
            ((DefaultMutableTreeNode) node.getParent()).add(menuNodeNew);
            this.refreshTree();
            jTree1.expandPath(new TreePath(((DefaultMutableTreeNode) node).getPath()));
            jScrollPane1.repaint();

            this.dispose();
        } catch (Exception ex) {
            Logger.getLogger(AddFoodDialog.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        JFileChooser fileChooser = new JFileChooser();
        int val = fileChooser.showOpenDialog(AddFoodDialog.this);
        if (val == JFileChooser.APPROVE_OPTION) {
            photoPath = fileChooser.getCurrentDirectory().toString() + "\\" + fileChooser.getSelectedFile().getName();
            ImageIcon ii = new ImageIcon(photoPath);
            ii.setImage(ii.getImage().getScaledInstance(160, 80, Image.SCALE_DEFAULT));

            photoLabel.setIcon(ii);
            photoLabel.repaint();
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void refreshTree() {
        DefaultMutableTreeNode rootNode;
        DefaultTreeModel tm;
        //tree
        rootNode = new DefaultMutableTreeNode("菜谱");
        tm = new DefaultTreeModel(rootNode);
        jTree1.setModel(tm);

        TFoodTypeJpaController jpa = new TFoodTypeJpaController();
        List<TFoodType> tfoodTypeList = jpa.findTFoodTypeEntities();
        for (TFoodType fd : tfoodTypeList) {
            DefaultMutableTreeNode foodTypeNode = new DefaultMutableTreeNode(fd);
            rootNode.add(foodTypeNode);

            for (TMenu menu : fd.getTMenuCollection()) {
                DefaultMutableTreeNode menuNode = new DefaultMutableTreeNode(menu);
                foodTypeNode.add(menuNode);
            }
        }
        jTree1.expandPath(new TreePath(((DefaultMutableTreeNode) rootNode).getPath()));
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea foodDesc;
    private javax.swing.JTextField foodNameText;
    private javax.swing.JTextField foodTypeText;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel photoLabel;
    private javax.swing.JTextField priceText;
    private com.ordering.pojo.TMenu tMenu1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
